package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.dto.WmMaterialDto;
import com.heima.file.MinIOTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.dao.WmMaterialDao;
import com.heima.service.WmMaterialService;
import com.heima.utils.WmThreadLocalUtil;
import org.checkerframework.checker.units.qual.min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 自媒体素材表(WmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 16:38:33
 */
@Service("wmMaterialService")
public class WmMaterialServiceImpl implements WmMaterialService {
    @Autowired
    private MinIOTemplate miniotemplate;
    @Resource
    private WmMaterialDao wmMaterialDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WmMaterial queryById(Long id) {
        return this.wmMaterialDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param wmMaterial 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
  /*  @Override
    public Page<WmMaterial> queryByPage(WmMaterial wmMaterial, PageRequest pageRequest) {
        long total = this.wmMaterialDao.count(wmMaterial);
        return new PageImpl<>(this.wmMaterialDao.queryAllByLimit(wmMaterial, pageRequest), pageRequest, total);
    }*/

    /**
     * 新增数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial insert(WmMaterial wmMaterial) {
        this.wmMaterialDao.insert(wmMaterial);
        return wmMaterial;
    }

    /**
     * 修改数据
     *
     * @param wmMaterial 实例对象
     * @return 实例对象
     */
    @Override
    public WmMaterial update(WmMaterial wmMaterial) {
        this.wmMaterialDao.update(wmMaterial);
        return this.queryById(wmMaterial.getId());
    }



    /**
     * 上传素材
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPic(MultipartFile multipartFile) throws IOException {
        //1. 校验参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2. 上传图片到minio
        String originalFilename = multipartFile.getOriginalFilename();
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        fileName = fileName + suffix;
        String url = miniotemplate.uploadImgFile("", fileName, multipartFile.getInputStream());
        //3. 保存素材数据到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(Long.valueOf(WmThreadLocalUtil.getUser().getId()));
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUpdatedTime(new Date());
        wmMaterialDao.insert(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 分页查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1. 校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //2. 执行分页条件查询
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(Long.valueOf(WmThreadLocalUtil.getUser().getId()));
        if (dto.getIsCollection()!=null&&dto.getIsCollection()==1){
            wmMaterial.setIsCollection(dto.getIsCollection());
        }
        //2.2 开启分页查询
        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<WmMaterial> page=wmMaterialDao.queryAll(wmMaterial);
        //2.1 封装分页参数
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getResult());
        return pageResponseResult;
    }
    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.wmMaterialDao.deleteById(id) > 0;
    }
    /**
     * 收藏素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Long id) {
        WmMaterial wmMaterial = wmMaterialDao.queryById(id);
        wmMaterial.setIsCollection(1);
        wmMaterialDao.update(wmMaterial);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    /**
     * 取消收藏素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel(Long id) {
        WmMaterial wmMaterial = wmMaterialDao.queryById(id);
        wmMaterial.setIsCollection(0);
        wmMaterialDao.update(wmMaterial);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
